<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/30
 * Time: 17:35
 */
class CtlLogin extends Controller
{
    public function login()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $data = $this->R("data");
            if (empty($data["mail_address"]) || empty($data["psd"])) {
                $this->out["error_info"] = "请输入登录信息";
                $this->tpl = "login.tpl";
            }else {
                //添加到数据库
                $mod = new ModRecordLog();
                $result = $mod->checkLogin($data);
                if ($result) {
                    //添加session
                    $info["user_name"] = $result[0]["user_name"];
                    $info["time"] = time();
                    $_SESSION["token"] = serialize($info);
                    header("Location: " . APP_URL); //跳转到首页
                    exit;
                }
                $this->out["error_info"] = "邮件不存在或者密码错误";
                $this->tpl = "login.tpl";
            }
        }else {
            $this->tpl = 'login.tpl';
        }
    }

    public function unLogin(){
        unset( $_SESSION["token"]);
        header("Location: " . APP_URL); //跳转到首页
        exit;
    }
}